package com.officeame.adapters;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.officegame.R;
import com.officegame.entities.GameTable;

public class TablesAdapter extends BaseAdapter {

	private Context context;
	private LayoutInflater inflater;
	private List<GameTable> tables;
	
	
	public TablesAdapter(Context context) {
		this.context = context;
		inflater = LayoutInflater.from(context);
		tables = new ArrayList<GameTable>();
	}
	
	@Override
	public int getCount() {
		return tables.size();
	}

	@Override
	public Object getItem(int location) {
		if (tables.size() < location) {
			return null;
		}
		return tables.get(location);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		TableViewHolder holder;
		String tableName = tables.get(position).getName();
		
		if (view != null) {
			holder = (TableViewHolder) view.getTag();
			holder.getTvTableName().setText(tableName);
		} else {
			view = inflater.inflate(R.layout.listitem_table, null);
			holder = new TableViewHolder();
			
			TextView tvTableName = (TextView) view.findViewById(R.id.tv_table_name);

			tvTableName.setText(tableName);
			holder.setTvTableName(tvTableName);
			
			view.setTag(holder);
		}
		
		return view;
	}
	
	public void refresh(List<GameTable> tables) {
		this.tables = tables;
		notifyDataSetChanged();
	}
	
	static class TableViewHolder {
		TextView tvTableName;

		public TextView getTvTableName() {
			return tvTableName;
		}

		public void setTvTableName(TextView tvTableName) {
			this.tvTableName = tvTableName;
		}
	}

}
